package vn.edu.ptithcm.pim.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import vn.edu.ptithcm.pim.dom.Customer;

public class CustomerValidator implements Validator {

	public boolean supports(@SuppressWarnings("rawtypes") Class clazz) {
		return Customer.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		Customer customer = (Customer) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
				"warning.required.customer.name");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address",
				"warning.required.customer.address");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
				"warning.required.customer.email");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "introduction",
				"warning.required.customer.introduction");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description",
				"warning.required.customer.description");
		if (customer.getCity().getCityid() == 0) {
			errors.rejectValue("city", "warning.required.customer.city");
		}
	}

}
